package cz.muni.fi.pa165.musiclibrary.musiclibraryweb.components;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.spring.injection.annot.SpringBean;

import cz.muni.fi.pa165.musiclibrary.api.service.IArtistService;
import cz.muni.fi.pa165.musiclibrary.api.service.auth.IUser;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IArtistDTO;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.MusicLibrarySession;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.utils.DTOFactory;

/**
 * Panel that contains form for adding or editing an artist.
 * After successful operation an event with payload {@link FormSubmittedEvent}
 * is sent.
 * 
 * @author Maros Kucbel
 * @date 2012-11-25T19:52:21+0100
 */
public final class ArtistFormPanel extends Panel {

    private IArtistDTO artist;
    @SpringBean
    private IArtistService artistService;
    
    public ArtistFormPanel(String id) {
        this(id, DTOFactory.create(IArtistDTO.class));
    }
    
    public ArtistFormPanel(String id, IArtistDTO artist) {
        super(id);
        this.artist = artist;
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.initComponents();
    }
    
    private void initComponents() {
        final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        feedbackPanel.setOutputMarkupId(true);
        this.add(feedbackPanel);
        
        Form<IArtistDTO> form = new Form<IArtistDTO>(ComponentIDs.FORM, Model.of(this.artist)) {

            @Override
            protected void onSubmit() {
                IArtistDTO artist = this.getModelObject();
                try {
                    IUser user = MusicLibrarySession.get().getUser();
                    String userName = user != null ? user.getUserName() : null;
                    if (artist.getIdArtist() != null) {
                        ArtistFormPanel.this.artistService.update(artist, userName);
                    } else {
                        ArtistFormPanel.this.artistService.create(artist, userName);
                    }
                    this.send(this.getPage(), Broadcast.BREADTH, new FormSubmittedEvent(RequestCycle.get().find(AjaxRequestTarget.class)));
                } catch (Exception ex) {
                    this.error(new StringResourceModel("database.error", this, null).getObject());
                    RequestCycle.get().find(AjaxRequestTarget.class).add(feedbackPanel);
                }
            }
            
        };
        RequiredTextField<String> nameField = new RequiredTextField<String>(ComponentIDs.NAME, new PropertyModel<String>(form.getModelObject(), "name"));
        nameField.setOutputMarkupId(true);
        form.add(nameField);
        form.add(new TextArea<String>(ComponentIDs.DESCRIPTION, new PropertyModel<String>(form.getModelObject(), "description")));
        form.add(new AjaxSubmitLinkWithErrorClass(ComponentIDs.SUBMIT_BUTTON, form) {

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.add(feedbackPanel);
                super.onError(target, form);
            }
        });
        this.add(form);
    }
    
    private static final class ComponentIDs {
        private static final String FORM = "form";
        private static final String NAME = "name";
        private static final String DESCRIPTION = "description";
        private static final String SUBMIT_BUTTON = "submit";
    }
}